<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>校园志愿者管理系统 - 登录</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3a0ca3;
            --accent-color: #4cc9f0;
            --success-color: #2ec4b6;
            --light-bg: #f8f9fa;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
            padding: 20px;
        }

        .login-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            overflow: hidden;
            max-width: 400px;
            width: 100%;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }

        .login-header {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            padding: 30px 20px;
            text-align: center;
        }

        .login-header h2 {
            font-weight: 700;
            margin-bottom: 5px;
        }

        .login-header p {
            opacity: 0.9;
            margin-bottom: 0;
        }

        .logo-icon {
            font-size: 3rem;
            margin-bottom: 15px;
            color: var(--accent-color);
        }

        .login-body {
            padding: 30px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            font-weight: 500;
            color: #495057;
            margin-bottom: 8px;
        }

        .form-control {
            border-radius: 10px;
            padding: 12px 15px;
            border: 2px solid #e9ecef;
            transition: all 0.3s;
        }

        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.25rem rgba(67, 97, 238, 0.15);
        }

        .login-btn {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            border: none;
            border-radius: 10px;
            padding: 12px;
            font-size: 16px;
            font-weight: 600;
            transition: all 0.3s;
            width: 100%;
            margin-top: 10px;
        }

        .login-btn:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(67, 97, 238, 0.3);
        }

        .login-btn:disabled {
            background: #6c757d;
            cursor: not-allowed;
        }

        .error-message {
            background-color: #f8d7da;
            color: #721c24;
            padding: 12px;
            border-radius: 8px;
            margin-top: 15px;
            display: none;
            border: 1px solid #f5c6cb;
        }

        .campus-elements {
            position: absolute;
            z-index: -1;
            opacity: 0.1;
        }

        .element-1 {
            top: 10%;
            left: 10%;
            font-size: 5rem;
        }

        .element-2 {
            bottom: 15%;
            right: 10%;
            font-size: 4rem;
        }

        .system-info {
            text-align: center;
            margin-top: 20px;
            color: #6c757d;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
<!-- 装饰元素 -->
<div class="campus-elements element-1">
    <i class="bi bi-mortarboard"></i>
</div>
<div class="campus-elements element-2">
    <i class="bi bi-book"></i>
</div>

<div class="login-card">
    <div class="login-header">
        <div class="logo-icon">
            <i class="bi bi-people-fill"></i>
        </div>
        <h2>校园志愿者管理系统</h2>
        <p>连接爱心，服务校园</p>
    </div>

    <div class="login-body">
        <form id="loginForm">
            <div class="form-group">
                <label for="username" class="form-label">学号</label>
                <div class="input-group">
                        <span class="input-group-text bg-light border-end-0">
                            <i class="bi bi-person"></i>
                        </span>
                    <input type="text" id="username" name="username" class="form-control border-start-0" placeholder="请输入您的学号" required>
                </div>
            </div>

            <div class="form-group">
                <label for="password" class="form-label">密码</label>
                <div class="input-group">
                        <span class="input-group-text bg-light border-end-0">
                            <i class="bi bi-lock"></i>
                        </span>
                    <input type="password" id="password" name="password" class="form-control border-start-0" placeholder="请输入密码" required>
                </div>
            </div>

            <button type="submit" class="login-btn">
                <i class="bi bi-box-arrow-in-right me-2"></i>登录系统
            </button>

            <div id="errorMessage" class="error-message"></div>
        </form>

        <div class="system-info">
            <p class="mb-0">
                <i class="bi bi-shield-lock me-1"></i>
                校园志愿服务一体化管理平台
            </p>
        </div>
    </div>
</div>

<script>
    document.getElementById('loginForm').addEventListener('submit', function(e) {
        e.preventDefault();

        const username = document.getElementById('username').value.trim();
        const password = document.getElementById('password').value;
        const errorMessage = document.getElementById('errorMessage');

        // 清除之前的错误信息
        errorMessage.style.display = 'none';

        // 基本验证
        if (!username || !password) {
            errorMessage.textContent = '请输入学号和密码';
            errorMessage.style.display = 'block';
            return;
        }

        // 学号格式验证
        if (!/^\d{8,12}$/.test(username)) {
            errorMessage.textContent = '学号格式不正确，请输入8-12位数字';
            errorMessage.style.display = 'block';
            return;
        }

        // 添加加载状态
        const loginBtn = document.querySelector('.login-btn');
        const originalBtnText = loginBtn.innerHTML;
        loginBtn.innerHTML = '<i class="bi bi-arrow-repeat me-2 spinner"></i>登录中...';
        loginBtn.disabled = true;

        // 发送登录请求到后端
        fetch('/user/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                username: username,
                password: password
            })
        })
            .then(response => {
                return response.json();
            })
            .then(data => {
                if (data.code === 200) {
                    // 登录成功
                    loginBtn.innerHTML = '<i class="bi bi-check-circle me-2"></i>登录成功';
                    setTimeout(() => {
                        window.location.href = '/index.html';
                    }, 500);
                } else if (data.code === 5000) {
                    // 登录失败
                    errorMessage.textContent = data.msg || '登录失败，请检查用户名和密码';
                    errorMessage.style.display = 'block';
                    loginBtn.innerHTML = originalBtnText;
                    loginBtn.disabled = false;
                } else {
                    // 其他未知状态
                    errorMessage.textContent = '登录异常，请稍后重试';
                    errorMessage.style.display = 'block';
                    loginBtn.innerHTML = originalBtnText;
                    loginBtn.disabled = false;
                }
            })
            .catch(error => {
                console.error('登录错误:', error);
                errorMessage.textContent = '网络错误，请检查网络连接后重试';
                errorMessage.style.display = 'block';
                loginBtn.innerHTML = originalBtnText;
                loginBtn.disabled = false;
            });
    });

    // 添加回车键支持
    document.addEventListener('keypress', function(e) {
        if (e.key === 'Enter' &&
            (document.activeElement.id === 'username' || document.activeElement.id === 'password')) {
            document.getElementById('loginForm').dispatchEvent(new Event('submit'));
        }
    });

    // 添加旋转动画类
    const style = document.createElement('style');
    style.textContent = `
            @keyframes spin {
                0% { transform: rotate(0deg); }
                100% { transform: rotate(360deg); }
            }
            .spinner {
                animation: spin 1s linear infinite;
            }
        `;
    document.head.appendChild(style);
</script>
</body>
</html>
